home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / dev / basic / SDSTCPLib.lha / SDSTCPLibrary / Examples / TCPMUIHTTPExample.ASC < prev    next >
Encoding:
Text File  |  1992-04-24  |  15.4 KB  |  469 lines

  1. ;
  2. ; HTTPGet - V1.00 Written by Rob Hutchinson for the Blitz TCP Lib as
  3. ; an example. Also very useful program :) I plan to expand it Soon.
  4. ;
  5. ; Note that you need the NEW version of the EFMUILib (v2) to compile
  6. ; this code. http://www.satanicdreams.com/
  7. ;
  8.  
  9.  
  10.   ;
  11.   ; About strings.
  12.   ;
  13.   a$="HTTPGet - (C)opyright By Rob Hutchinson"+Chr$(10)
  14.   a$+"Version: 1.00 [020+]"+Chr$(10)
  15.   a$+"Compilation date: 18.03.2000"+Chr$(10)
  16.   a$+Chr$(10)
  17.   a$+Chr$(27)+"bFREEWARE"+Chr$(10)
  18.   a$+Chr$(27)+"n"+Chr$(10)
  19.   a$+"Satanic Dreams Software Homepages:"+Chr$(10)
  20.   a$+"http://www.satanicdreams.com/"+Chr$(10)+Chr$(10)
  21.   a$+"Rob Hutchinson <"+Chr$(27)+"8rob@satanicdreams.com"+Chr$(27)+"0>"+Chr$(10)
  22.   a$+"Special thanks to:"+Chr$(10)
  23.   a$+"David McMinn"+Chr$(10)
  24.   a$+"Douglas McLaughlin"
  25.  
  26.   WBStartup
  27.   !StackOpen{6000}
  28.  
  29.   FindScreen 0
  30.   MaxLen pat$=512
  31.   pat$="RAM:"
  32.  
  33.   If ReadFile(0,"HTTPGet.CFG")
  34.     FileInput 0
  35.     LastPath$=Edit$(512)
  36.     LastFile$=Edit$(2000)
  37.     LastChex$=Edit$(512)
  38.     CloseFile 0
  39.   EndIf
  40.  
  41.   DefaultOutput
  42.  
  43.   pat$=LastPath$
  44.  
  45.   ;####################################################################
  46.  
  47.   MUIApplicationTitle       "HTTPGet"
  48.   MUIApplicationVersion     "$VER: HTTPGet 1.00 (18.03.2000)"
  49.   MUIApplicationCopyright   "(C)1999 SDS, Robert Hutchinson."
  50.   MUIApplicationAuthor      "Robert Hutchinson & David McMinn"
  51.   MUIApplicationDescription "Download files via HTTP from the net."
  52.   MUIApplicationBase        "HTTPGET"
  53.  
  54.   #BME_APP                         = 1
  55.     #BME_Window1                   = 2
  56.       #BME_MainGroup               = 3
  57.         #BME_Text                  = 4
  58.         #BME_PathGroup             = 5
  59.           #BME_PathLabel           = 6
  60.           #BME_PathSmaller         = 7
  61.             #BME_PathString        = 8
  62.             #BME_PathPoper         = 9
  63.  
  64.         #BME_GroupStr              = 10
  65.           #BME_ViewLabel           = 11
  66.           #BME_ViewCheck           = 12
  67.           #BME_GetFileLabel        = 13
  68.           #BME_GetString           = 14
  69.           #BME_GetButton           = 15
  70.         #BME_GroupPro              = 16
  71.           #BME_LabelProgress       = 17
  72.           #BME_GaugeProgress       = 18
  73.         #BME_AbortButton           = 19
  74.  
  75.   #EB_CloseRequest                 = -1
  76.   #EB_Ack                          = 10
  77.   #EB_GoGetIt                      = 11
  78.   #EB_AbortIt                      = 12
  79.   #EB_PopDrawer                    = 13
  80.  
  81.   #HTP_MenuList_MenuList           = 20
  82.     #HTP_Menu_HTTPGet              = 21
  83.       #HTP_MenuItem_Iconify        = 5000
  84.       #HTP_MenuItem_AboutHTP       = 5006
  85.         #HTP_UData_AboutHTP        = 30
  86.       #HTP_MenuItem_AboutMUI       = 5007
  87.       #HTP_MenuItem_Quit           = 5008
  88.  
  89.     #HTP_Menu_Transfer             = 22
  90.       #HTP_MenuItem_Retrieve       = 5020
  91.         #HTP_UData_Retrieve        = 31
  92.       #HTP_MenuItem_AbortTransfer  = 5021
  93.         #HTP_UData_AbortTransfer   = 32
  94.  
  95.     #HTP_Menu_Settings             = 23
  96.       #HTP_MenuItem_SaveSetup      = 5062
  97.  
  98.   #BME_ShowReqHere                 = 24
  99.  
  100.  
  101.   GTMenuTitle 0,0,"HTTPGet"
  102.     GTMenuItem  0,0,0,0,"Iconify","I",0,#HTP_MenuItem_Iconify
  103.     GTMenuItem  0,0,0,1
  104.     GTMenuItem  0,0,0,2,"About HTTPGet...","?",0,#HTP_MenuItem_AboutHTP
  105.     GTMenuItem  0,0,0,3,"About MUI...","",0,#HTP_MenuItem_AboutMUI
  106.     GTMenuItem  0,0,0,4
  107.     GTMenuItem  0,0,0,5,"Quit","Q",0,#HTP_MenuItem_Quit
  108.  
  109.   GTMenuTitle 0,1,"Transfer"
  110.     GTMenuItem  0,0,1,0,"Retrieve File","R",0,#EB_GoGetIt ;#HTP_MenuItem_Retrieve
  111.     GTMenuItem  0,0,1,1,"Abort Transfer","A",0,#EB_AbortIt ;#HTP_MenuItem_AbortTransfer
  112.  
  113.   GTMenuTitle 0,2,"Settings"
  114.     GTMenuItem  0,0,2,0,"Save Setup","S",0,#HTP_MenuItem_SaveSetup
  115.  
  116.  
  117.   ;#########################################################################
  118.  
  119.   Macro di  MUISet `1,#MUIA_Disabled,True  End Macro
  120.   Macro en  MUISet `1,#MUIA_Disabled,False End Macro
  121.   Macro mdi MUISet `1,#MUIA_Menuitem_Enabled,0 End Macro
  122.   Macro men MUISet `1,#MUIA_Menuitem_Enabled,1 End Macro
  123.  
  124.  
  125.   MUIText              #BME_Text,"Idle!",True
  126.  
  127.     MUILabel           #BME_PathLabel,"To _Path:",0
  128.     MUIKeyBetterString #BME_PathString,LastPath$,512,"t"
  129.     MUIPopButton       #BME_PathPoper,#MUII_PopDrawer
  130.   MUIAddObjsHGroup     #BME_PathSmaller,#BME_PathString,#BME_PathPoper
  131.   MUICreateHGroup      #BME_PathSmaller
  132.   MUIGroupHSpacing     #BME_PathSmaller,1
  133.   MUIAddObjsHGroup     #BME_PathGroup,#BME_PathLabel,#BME_PathSmaller
  134.   MUICreateHGroup      #BME_PathGroup
  135.  
  136.     MUILabel           #BME_ViewLabel,"_View:",0
  137.     MUIKeyCheckMark    #BME_ViewCheck,Val(LastChex$),"v"
  138.     MUILabel           #BME_GetFileLabel,"Get _File:",0
  139.     MUIAddTags         #BME_GetString,#MUIA_String_AdvanceOnCR,True
  140.     MUIKeyBetterString #BME_GetString,LastFile$,2000,"f"
  141.     MUISimpleButton    #BME_GetButton,"_Retrieve!"
  142.     MUISet             #BME_GetButton,#MUIA_Text_SetVMax,False
  143.     MUIWeight          #BME_GetButton,5
  144.   MUIAddObjsHGroup     #BME_GroupStr,#BME_ViewLabel,#BME_ViewCheck,#BME_GetFileLabel,#BME_GetString,#BME_GetButton
  145.   MUICreateHGroup      #BME_GroupStr
  146.  
  147.     MUILabel           #BME_LabelProgress,"Progress:",0
  148.     MUIAddTags         #BME_GaugeProgress,#MUIA_Text_PreParse,Null(Chr$(27)+"c")
  149.     MUIText            #BME_GaugeProgress,"Welcome to HTTPGet V1.00 - TCP Example",True
  150.  
  151.   MUIAddObjsHGroup     #BME_GroupPro,#BME_LabelProgress,#BME_GaugeProgress
  152.   MUICreateHGroup      #BME_GroupPro
  153.  
  154.   MUISimpleButton      #BME_AbortButton,"Abort Transfer!"
  155.   !di{#BME_AbortButton}
  156.  
  157.   MUIAddObjsVGroup  #BME_MainGroup,#BME_Text,#BME_PathGroup,#BME_GroupStr,#BME_GroupPro,#BME_AbortButton
  158.   MUICreateVGroup   #BME_MainGroup
  159.  
  160.   MUICycleChains    1,#BME_PathString,#BME_PathPoper,#BME_ViewCheck,#BME_GetString,#BME_GetButton,#BME_AbortButton
  161.  
  162.   MUIShortHelp      #BME_PathString,"A path on disk to download the file(s) to."+Chr$(10)+"Files will take the download name."
  163.   MUIShortHelp      #BME_PathPoper,"Select a directory from disk with a path requester."
  164.  MUIShortHelp #BME_ViewCheck,"View file with multiview after downloading."+Chr$(10)+"Note that partial files will be displayed."
  165.   MUIShortHelp      #BME_GetString,"Put the URL of the path here."+Chr$(10)+"Should always start with 'http://'"
  166.   MUIShortHelp      #BME_GetButton,"Get the entered file from the net."+Chr$(10)+"This will fail if your not online."
  167.   MUIShortHelp #BME_AbortButton,"Abort current transfer."+Chr$(10)+"This button is only active when a download is in progress."
  168.  
  169.   MUICreateMenu     #HTP_MenuList_MenuList,0,0,#HTP_Menu_HTTPGet,#HTP_Menu_Transfer,#HTP_Menu_Settings
  170.   MUISetMenu        #BME_Window1,#HTP_MenuList_MenuList
  171.  
  172.   MUICreateWindow   #BME_Window1,"TCP - HTTPGet V1.00: By Rob Hutchinson 2000","WIN1",#BME_MainGroup
  173.   MUIAddSubWindow   #BME_Window1
  174.  
  175.   ;###########################################################################
  176.  
  177.   If MUICreateApplication<>True  ; If the application fails to be generated,
  178.     JMP Do_ProgramClose          ; we terminate the task.
  179.   EndIf                          ; Note: This does not open the window.
  180.  
  181.   MUIApplicationObj #BME_APP
  182.  
  183.   MUIFindUData  #HTP_UData_Retrieve,#HTP_MenuList_MenuList,#EB_GoGetIt ;#HTP_MenuItem_Retrieve
  184.   MUIFindUData  #HTP_UData_AbortTransfer,#HTP_MenuList_MenuList,#EB_AbortIt ;#HTP_MenuItem_AbortTransfer
  185.   MUIFindUData  #HTP_UData_AboutHTP,#HTP_MenuList_MenuList,#HTP_MenuItem_AboutHTP
  186.  
  187.   MUINotifyApp  #BME_Window1,#MUIA_Window_CloseRequest,1,#EB_CloseRequest
  188.   MUINotifyApp  #BME_GetButton,#MUIA_Pressed,0,#EB_GoGetIt
  189.   MUINotifyApp  #BME_AbortButton,#MUIA_Pressed,0,#EB_AbortIt
  190.   MUINotifyApp  #BME_PathPoper,#MUIA_Pressed,0,#EB_PopDrawer
  191.  
  192.   If MUIOpenWindow(#BME_Window1)=False
  193.     JMP Do_ProgramClose
  194.   EndIf
  195.  
  196.   !mdi{#HTP_UData_AbortTransfer}
  197.  
  198.   FSize.l=0
  199.  
  200.  
  201.  
  202.   Repeat
  203.     Delay_ 1
  204.     Mev.l=MUIEvent
  205.     If DoEventsNow.l
  206.       Tev.l=TCPEvent
  207.     Else
  208.       Tev.l=0
  209.     EndIf
  210.  
  211.     Select Mev
  212.       Case #EB_GoGetIt
  213.  
  214.         HTTPFile$=MUIGetString$(#BME_GetString)
  215.         If Len(HTTPFile$)<10
  216.           MUITextContents #BME_Text,"Host too short!"
  217.         Else
  218.           If UCase$(Left$(HTTPFile$,7))<>"HTTP://"
  219.             MUITextContents #BME_Text,"Incorrect address! No HTTP://"
  220.           Else
  221.             h=Instr(HTTPFile$,"/",8)
  222.             host$=Mid$(HTTPFile$,8,h-8)
  223.             doc$=Mid$(HTTPFile$,h,Len(HTTPFile$))
  224.             countit.w=0
  225.  
  226.             For a.w=Len(HTTPFile$) To 1 Step -1
  227.               countit.w+1
  228.               mi$=Mid$(HTTPFile$,a,1)
  229.               If mi$="/"
  230.                 HIFile$=Right$(HTTPFile$,(countit.w-1))
  231.                 a=1
  232.               EndIf
  233.             Next a
  234.  
  235.             getstr$="GET "+HTTPFile$+Chr$(13)+Chr$(10)
  236.  
  237.             If TCPOpen
  238.               fail.l=TCPCreateSocket(0,Off,#TCP_SOCK_Read|#TCP_SOCK_Write,20,0)
  239.               If fail.l = #TCP_SOCK_Ok
  240.  
  241.                 MUISet #BME_Window1,#MUIA_Window_Sleep,True
  242.                 MUITextContents #BME_Text,"Looking up hostname..."
  243.                 OurSockStatus.l=TCPConnectSocket(0,host$,80)
  244.                 MUISet #BME_Window1,#MUIA_Window_Sleep,False
  245.                 If OurSockStatus.l=#TCP_CONN_CONNECTING
  246.                   DoEventsNow.l=1
  247.                   !en{#BME_AbortButton}
  248.                   !men{#HTP_UData_AbortTransfer}
  249.                   !mdi{#HTP_UData_Retrieve}
  250.                   !mdi{#HTP_UData_AboutHTP}
  251.                   !di{#BME_GroupStr}
  252.                   !di{#BME_PathGroup}
  253.                   MUITextContents #BME_Text,"Host found!..."
  254.                 Else
  255.                   Select OurSockStatus.l
  256.                     Case #TCP_CONN_FailHostResolve
  257.                       MUITextContents #BME_Text,"Unable to resolve hostname. FAILED!"
  258.                     Case #TCP_CONN_FailNoConnect
  259.                       MUITextContents #BME_Text,"Not connected. FAILED!"
  260.                     Case #TCP_CONN_FailError
  261.                       MUITextContents #BME_Text,"Fatal error. FAILED!"
  262.                   End Select
  263.                   TCPRemoveSocket 0
  264.                 EndIf
  265.  
  266.               Else
  267.                 ;
  268.                 ; Some failures.
  269.                 ;
  270.                 Select fail.l
  271.                   Case #TCP_SOCK_FailTimeout
  272.                     MUITextContents #BME_Text,"Could not set timeout. FAILED!"
  273.                   Case #TCP_SOCK_FailAsync
  274.                     MUITextContents #BME_Text,"Could not set async. FAILED!"
  275.                   Case #TCP_SOCK_FailNoSock
  276.                     MUITextContents #BME_Text,"Could not create sock. FAILED!"
  277.                 End Select
  278.               EndIf
  279.             Else
  280.               MUITextContents #BME_Text,"No BSDSocket.library. Please Start TCP/IP Stack!"
  281.             EndIf
  282.  
  283.           EndIf
  284.         EndIf
  285.  
  286.       Case #EB_AbortIt
  287.         If TCPAvail
  288.           CloseFile 0
  289.           FSStr$="Got "+Str$(FSize.l)+" bytes! Average CPS: "+Str$(CPS.l)+" (Partial File)."
  290.           !di{#BME_AbortButton}
  291.           !mdi{#HTP_UData_AbortTransfer}
  292.           !men{#HTP_UData_Retrieve}
  293.           !men{#HTP_UData_AboutHTP}
  294.           !en{#BME_GroupStr}
  295.           !en{#BME_PathGroup}
  296.           MUISet #BME_GaugeProgress,#MUIA_Text_Contents,&FSStr$
  297.           FSize.l=0
  298.           CPS.l=0
  299.           TCPRemoveSocket 0
  300.           DoEventsNow.l=0
  301.           Tev=0
  302.           TCPClose
  303.           DefaultOutput
  304.           MUITextContents #BME_Text,"Transfer interupted!..."
  305.           JSR Do_Read
  306.         Else
  307.           MUITextContents #BME_Text,"Cannot abort right now!..."
  308.         EndIf
  309.  
  310.       Case #EB_PopDrawer
  311.         MUISet #BME_Window1,#MUIA_Window_Sleep,True
  312.         NPa$=ASLPathRequest$("Select a download path..",pat$)
  313.         If NPa$<>""
  314.           MUISetString #BME_PathString,NPa$
  315.         EndIf
  316.         MUISet #BME_Window1,#MUIA_Window_Sleep,False
  317.  
  318.    ;###########################################################################################
  319.  
  320.       Case #HTP_MenuItem_Iconify
  321.         MUISet #BME_APP,#MUIA_Application_Iconified,True
  322.  
  323.       Case #HTP_MenuItem_AboutHTP
  324.         MUIRequest #BME_Window1,0,"HTTPGet, Program Information","Ok, I Read it!",Chr$(27)+"c"+a$
  325.  
  326.       Case #HTP_MenuItem_AboutMUI
  327.         MUIDoMethod #BME_APP,#MUIM_Application_AboutMUI,MUIObjLoc(#BME_Window1)
  328.  
  329.       Case #HTP_MenuItem_Quit
  330.         Mev = #EB_CloseRequest
  331.  
  332.       Case #HTP_MenuItem_SaveSetup
  333.         JSR Do_SaveSetup
  334.  
  335.     End Select
  336.  
  337.     If Tev>0
  338.       If (OurSockStatus.l=#TCP_CONN_CONNECTING) AND TCPIsReadEvent(0) OR TCPIsWriteEvent(0)
  339.         err.l=TCPSocketError(0)
  340.         If err
  341.           JSR Do_Error
  342.         Else
  343.           OurSockStatus.l=0
  344.           ;
  345.           ; Now connected..
  346.           ;
  347.           MUITextContents #BME_Text,"Connected to server: "+host$
  348.           TCPSetStates    0,#TCP_SOCK_Read
  349.           TCPNPrint       0,getstr$
  350.           Outfile$=MUIGetString$(#BME_PathString)
  351.           Outfile$+HIFile$
  352.           If WriteFile(0,Outfile$)
  353.             FileOutput 0
  354.           Else
  355.             MUITextContents #BME_Text,"Write failed. Closing connection!"
  356.             !di{#BME_AbortButton}
  357.             !mdi{#HTP_UData_AbortTransfer}
  358.             !men{#HTP_UData_Retrieve}
  359.             !men{#HTP_UData_AboutHTP}
  360.             !en{#BME_GroupStr}
  361.             !en{#BME_PathGroup}
  362.             FSStr$="FAILED!"
  363.             MUISet #BME_GaugeProgress,#MUIA_Text_Contents,&FSStr$
  364.             DoEventsNow.l=0
  365.             FSize.l=0
  366.             CPS.l=0
  367.             TCPRemoveSocket 0
  368.             TCPClose
  369.             DefaultOutput
  370.           EndIf
  371.           TimerReset
  372.         EndIf
  373.       Else
  374.         If Tev
  375.           If TCPIsReadEvent(0)
  376.             err.l=TCPSocketError(0)
  377.             If err
  378.               JSR Do_Error
  379.             Else
  380.               Reed$=TCPReadSocket$(0)
  381.               If Reed$=""
  382.                 CloseFile 0
  383.                 MUITextContents #BME_Text,"Download complete..."
  384.                 FSStr$=Str$(FSize.l)+" bytes saved! Average CPS: "+Str$(CPS.l)
  385.                 !di{#BME_AbortButton}
  386.                 !mdi{#HTP_UData_AbortTransfer}
  387.                 !men{#HTP_UData_Retrieve}
  388.                 !men{#HTP_UData_AboutHTP}
  389.                 !en{#BME_GroupStr}
  390.                 !en{#BME_PathGroup}
  391.                 MUISet #BME_GaugeProgress,#MUIA_Text_Contents,&FSStr$
  392.                 DoEventsNow.l=0
  393.                 FSize.l=0
  394.                 CPS.l=0
  395.                 TCPRemoveSocket 0
  396.                 TCPClose
  397.                 DefaultOutput
  398.                 JSR Do_Read
  399.               Else
  400.                 FSize.l+Len(Reed$)
  401.                 DSecs.l=Ticks/50
  402.                 If DSecs.l<1 Then DSecs.l=1
  403.                 If FSize.l OR DSecs.l Then CPS.l=FSize.l/DSecs.l
  404.                 If CPS<0 Then CPS=0
  405.                 FSStr$="Bytes transfered: "+Str$(FSize.l)+" - "+Str$(CPS.l)+" CPS."
  406.                 MUISet #BME_GaugeProgress,#MUIA_Text_Contents,&FSStr$
  407.                 Print Reed$
  408.               EndIf
  409.             EndIf
  410.           EndIf
  411.         EndIf
  412.       EndIf
  413.     EndIf
  414.  
  415.   Until Mev = #EB_CloseRequest
  416.  
  417.  
  418.   Do_ProgramClose:
  419.     If DoEventsNow.l=1
  420.       CloseFile 0
  421.       TCPRemoveSocket 0
  422.       TCPClose
  423.     EndIf
  424.  
  425.     JSR Do_SaveSetup
  426.  
  427.     !StackClose
  428.     End
  429.  
  430.   Do_Read:
  431.     If MUIGet(#BME_ViewCheck,#MUIA_Selected)
  432.       Execute_ "Run >NIL: SYS:Utilities/Multiview "+Chr$(34)+Outfile$+Chr$(34),0,0
  433.     EndIf
  434.   RTS
  435.  
  436.   Do_Error:
  437.     MUITextContents #BME_Text,"ERROR #"+Str$(err)+": "+TCPErrorString$
  438.     ;
  439.     ; Close all socket stuff.
  440.     ;
  441.     CloseFile 0
  442.     !di{#BME_AbortButton}
  443.     !mdi{#HTP_UData_AbortTransfer}
  444.     !men{#HTP_UData_Retrieve}
  445.     !men{#HTP_UData_AboutHTP}
  446.     !en{#BME_GroupStr}
  447.     !en{#BME_PathGroup}
  448.     FSize.l=0
  449.     CPS.l=0
  450.     TCPRemoveSocket 0
  451.     DoEventsNow.l=0
  452.     Tev=0
  453.     TCPClose
  454.     DefaultOutput
  455.     MUITextContents #BME_Text,"Transfer interupted!..."
  456.   RTS
  457.  
  458.   Do_SaveSetup:
  459.     If WriteFile(1,"HTTPGet.CFG")
  460.       FileOutput 1
  461.       NPrint MUIGetString$(#BME_PathString)
  462.       NPrint MUIGetString$(#BME_GetString)
  463.       NPrint MUIGet(#BME_ViewCheck,#MUIA_Selected)
  464.       CloseFile 1
  465.     EndIf
  466.   RTS
  467.  
  468.  
  469.